var $;
var form;
var id =null;
// var index = parent.layer.getFrameIndex(window.name);//获取子窗口索引
layui.config({
	base : "js/"
}).use(['form','layer','jquery','upload'],function(){
	    form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage;
		$ = layui.jquery;

    layui.upload({
        elem:$('#uImg'),
    });

    $("#uImg").click(function(){
        var oldFilename =$("#bannnerImgText").val();
        layui.upload({
            exts: 'gif|jpg|jpeg|png|bmp|png',
            size: 2*1024,
            url : ctx+"/service/clientContents/upload_img?oldFileName="+oldFilename,
            success: function(res){
                var bannerImg = $("#bannerImg");
                bannerImg.css({"width":"60px"},{"height":"35px"});
                bannerImg.attr('src',res.data.url);
                $("#bannnerImgText").val(res.data.uri);
            }

        });
    });
 	form.on("submit(edit)",function(data){
        var dataObject = data.field;
        delete dataObject.banner_img;//删除多余的参数
        var pams = JSON.stringify(dataObject);
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:2000,shade:0.8});
        setTimeout(function(){
            $.ajax({
                type: "post",
                url: ctx + "/service/clientContents/update",
                data:pams,
                contentType: "application/json",
                async: false,
                success: function (data) {
                    top.layer.close(index);
                    if(data.status==0){
                        top.layer.msg(data.msg, {icon: 1,time:3000,});
                    }else {
                        top.layer.msg(data.msg, {icon: 2,time:3000,})
                    }
                }
            });
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        },2000);
 		return false;
 	});

})

//格式化时间
function formatTime(_time){
    var year = _time.getFullYear();
    var month = _time.getMonth()+1<10 ? "0"+(_time.getMonth()+1) : _time.getMonth()+1;
    var day = _time.getDate()<10 ? "0"+_time.getDate() : _time.getDate();
    var hour = _time.getHours()<10 ? "0"+_time.getHours() : _time.getHours();
    var minute = _time.getMinutes()<10 ? "0"+_time.getMinutes() : _time.getMinutes();
    return year+"-"+month+"-"+day+" "+hour+":"+minute;
}


function setId(id) {
    $.get(ctx + "/service/clientContents/findOne", {id: id}, function (response) {
        $('#id').val(response.data.id);
        $('#bannerTitle').val(response.data.bannerTitle);
        $('#bannerUrl').val(response.data.bannerUrl);
        if(response.data.bannerImg !=null){
            var bannerImg = $("#bannerImg");
            bannerImg.css({"width":"60px"},{"height":"35px"});
            bannerImg.attr('src',imgPath+'/banner/'+response.data.bannerImg);
            $("#bannnerImgText").val(response.data.bannerImg);
        }
        form.render();//刷新表单(很重要)

    });

}


